layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    var activityId = $("#activityId").val();
    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/activityItem/jsonList',
        where: { activityId: activityId},
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {type: 'numbers', title: '序号'},
            {field: 'itemTitle',minWidth:120, title: '商品名称',align:'center'},
            {field: 'activityTitle',minWidth:120, title: '活动名称',align:'center'},
            {field: 'brandName',minWidth:120, title: '品牌名称',align:'center'},
            {field: 'catName',minWidth:120, title: '品牌分类名称',align:'center'},
            {field: 'status',minWidth:120, title: '特色商品',align:'center',templet:function(d){
                if (d.status=='1'){
                    return '<input class="editStatus" checked="true" data-id="'+d.id+'" name="open" lay-skin="switch" lay-filter="switchTest" lay-event="editStatus" type="checkbox">';
                }else{
                    return '<input class="editStatus" name="open" data-id="'+d.id+'" lay-skin="switch" lay-filter="switchTest" lay-event="editStatus" type="checkbox">';
                }
            }},
            {title: '操作',width:170,align:"center", templet:function(d){
                return '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';
            }}
        ]]
    });

    $(".add_btn").click(function(){
        var index = layui.layer.open({
            title :"选择商品",
            type : 2,
            content : "/item/listSelectPage?activityId="+activityId,
            area:['600px','400px'],
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                //layui.layer.full(index);
            },
            end: function () {
                tableIns.reload();
            }
        });
    })

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                activityId: $("#activityId").val(),
                itemTitle:$("#itemTitle").val()
            }
        })
    });

    //列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'del'){ //删除
            layer.confirm('是否删除活动商品吗？',{icon:3, title:'提示信息'},function(index){
                $.get("/activityItem/deleteEntity",{
                    "id":data.id,
                    "_method":"DELETE"
                },function(result){
                    if(1==result.returnInfo.state){
                        tableIns.reload();
                        layer.close(index);
                    }else{
                        top.layer.msg(result.returnInfo.errorMsg);
                    }
                })
            });
        }
    });

    // 主键
    form.on('switch(switchTest)', function(obj){
        // 获取当前控件
        var id = $(this).attr("data-id");
        console.log(id);
        var status = "0";
        if (this.checked){
            status = "1";
        }
        $.get("/activityItem/updateStatus",{
            "id":id,
            "status":status,
            "_method":"DELETE"
        },function(result){
            if(1==result.returnInfo.state){
                tableIns.reload();
            }else{
                top.layer.msg(result.returnInfo.errorMsg);
            }
        })
    });
})